package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.role.Role;
import com.gxuwz.medical.tools.PageBean;
import com.gxuwz.medical.tools.StringUtil;

public class RoleDao {
	DbUtil dbUtil = new DbUtil(); // 数据库工具类

	/**
	 * 
	 * @Title: roleList @Description: 查询 @author 陈震洺 @param con @param
	 *         pageBean @param role @return @throws Exception @throws
	 */
	public ResultSet roleList(Connection con, PageBean pageBean, Role role) throws Exception {
		StringBuffer sb = new StringBuffer("select * from role where 1=1");
		/*
		 * StringBuffer sb = new StringBuffer(
		 * "SELECT * FROM role AS r , menu AS m , role_menu AS rm WHERE r.roleid = rm.roleid AND m.id = rm.menuid"
		 * );
		 */
		// grade的班级名不为空-->搜索操作
		if (role != null && !StringUtil.isEmpty(role.getRoleName())) {
			sb.append(" and roleName like '%" + role.getRoleName() + "%'"); //
		}

		if (pageBean != null) {
			sb.append(" limit " + pageBean.getStart() + "," + pageBean.getRows());
		}

		// 搜索操作-->把and 替换成where，不是搜索自然不会替换
		PreparedStatement pstmt = con.prepareStatement(sb.toString());
		System.out.println(pstmt.executeQuery());
		return pstmt.executeQuery();
	}

	/**
	 * 
	 * @Title: roleCount @Description: @author 陈震洺 @param con @param
	 *         role @return @throws Exception @throws
	 */
	public int roleCount(Connection con, Role role) throws Exception {
		StringBuffer sb = new StringBuffer("select count(*) as total from role");

		if (!StringUtil.isEmpty(role.getRoleName())) {
			sb.append(" and roleName like '%" + role.getRoleName() + "%'"); //
		}
		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			return rs.getInt("total");

		} else {
			return 0;
		}

	}

	/**
	 * 
	 * @Title: roleDelete @Description: 删除 @author 陈震洺 @param con @param
	 *         delIds @return @throws Exception @throws
	 */
	public int roleDelete(Connection con, String delIds) throws Exception {
		String sql = "delete from role where id in(" + delIds + ")";// in效率高

		PreparedStatement pstmt = con.prepareStatement(sql);
		return pstmt.executeUpdate();
	}

	public int roleAdd(Connection con, Role role) throws Exception {
		System.out.println("role.getRoleid()==" + role.getRoleid());
		System.out.println("role.getRoleName()==" + role.getRoleName());
		String sql = "insert role values(null,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, role.getRoleid());
		pstmt.setString(2, role.getRoleName());
		return pstmt.executeUpdate();
	}

	/**
	 * 
	 * @Title: gradeModify @Description: 修改 @author 陈震洺 @param con @param
	 *         role @return @throws Exception @throws
	 */
	public int roleModify(Connection con, Role role) throws Exception {
		String sql = "update role set roleName=?,roleid=? where id=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, role.getRoleName());
		pstmt.setString(2, role.getRoleid());
		pstmt.setInt(3, role.getId());
		return pstmt.executeUpdate();
	}

	/**
	 * 
	 * @Title: findById @Description: 根据id查询权限ID @author 陈震洺 @param
	 *         role @return @throws
	 */
	public List<Role> findById(Role role) {
		List<Role> list = new ArrayList<Role>(); // 创建集合

		String sql = "select * from role where id='" + role.getId() + "'"; // SQL查询语句
		try {
			Connection con = null;
			con = dbUtil.getCon();
			PreparedStatement pst = con.prepareStatement(sql);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				Role role1 = new Role();
				role1.setId(rst.getInt("id"));
				role1.setRoleid(rst.getString("roleid"));
				role1.setRoleName(rst.getString("roleName"));
				list.add(role1);
			}
			rst.close(); // 关闭
			pst.close(); // 关闭
		} catch (SQLException e) {
			e.printStackTrace(); // 抛出异常
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list; // 返回一个集合
	}

	public boolean SaveRoleAccredit(String RoleId, String menuid) throws Exception { // 添加信息
		String sql = "INSERT INTO `role_menu`(`id`,`roleid`,`menuid`) VALUES (null,?,?)"; // 添加的SQL语句
		Connection con = null;
		con = dbUtil.getCon();
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, RoleId);
			pst.setString(2, menuid);
			int count = pst.executeUpdate();
			pst.close();
			return count > 0 ? true : false; // 是否添加的判断
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
